package linearstructure.queue.ordinary;

/**
 * @ author : HyoJoo-W
 * @ createDate : 2021-05-23 14:03
 * @ Description : 队列
 */
public class Queue
{
    private final int maxSize;
    private int front;//队列头(指向第一个元素的前一个位置)
    private int rear;//队列尾(指向最后一个元素)
    private int[] arr;//队列数组,模拟队列

    public Queue(int arrMaxSize)
    {
        maxSize = arrMaxSize;
        arr = new int[maxSize];
        front = -1;
        rear = -1;
    }

    public boolean isFull()
    {
        return rear == maxSize - 1;
    }

    public boolean isEmpty()
    {
        return rear == front;
    }

    public void addQueue(int n)
    {
        if (isFull())
        {
            System.out.println("队列已满！");
            return;
        }
        rear++;
        arr[rear] = n;
    }

    public int getQueue()
    {
        if (isEmpty())
        {
            throw new RuntimeException("队列为空,不能取出数据！");
        }
        front++;
        return arr[front];
    }

    public void showQueue()
    {
        //遍历
        if (isEmpty())
        {
            System.out.println("队列为空,不能遍历！");
            return;
        }
        for (int i = 0; i < arr.length; i++)
        {
            System.out.printf("arr[%d]=%d\n", i, arr[i]);
        }
    }

    //显示头数据
    public int headQueue()
    {
        if (isEmpty())
        {
            System.out.println("队列为空");
            throw new RuntimeException("队列为空");
        }
        return arr[front + 1];
    }

}
